<?php

namespace app\admin;
use app\admin\model\setting\Setting as SettingModel;
use think\Request;
use think\facade\Cache;
use think\facade\Session;
use app\admin\common;
use think\facade\Request as facadeRequest;
class auth{
	public $error="";
	public $code="";
	 /**
     * 到时候前后端约定一端加密码一端解密
     * 
     */
    public $noCheckAuth=[
    	'loginMobile',
    	'uploadeImagess',
    	'exportOrder',
    	'findDetails',
    	'importOpus',
        'getUserNft'
    ];
   
    public function noCheckAuth(){
    	$url=facadeRequest::pathinfo();
    	$flag=0;
    	foreach($this->noCheckAuth as $k=>$v){
    		if(stripos($url,$v)===0 || stripos($url,$v)>0){
    			$flag=1;
    			break;
    		}
    	}
    	//如果该URL是在不需要验证当中则跳出验证
    	if($flag==1){
    		return true; 
    	}
    	return false;
    }
    
	public function check_token(){
		global $_admin;
		//1.根据token获取用户数据
		$checkAuth=$this->noCheckAuth();
		if($checkAuth==true) return true;
		$admin_obj=new \app\admin\model\index\Admin;
		$common=new common();
		$headerToken=$common->getToken();//获取前端传过来的token
		if($headerToken==false){
			$this->error=$common->error;
			$this->code=$common->code;
			return false;
		}
		$where=[
			'id'=>$headerToken[1]
		];
		//3.根据Id查用户数据
		$operation=$admin_obj->getAdmin($where);
		$_admin=$operation;
    	$token=$operation['token'];
    	//4.如果登陆已过期
    	if(time()>$operation['token_expiration']){
    		$this->error=config('adminMessage.login.loginExpiration.msg');
    		$this->code=config('adminMessage.login.loginExpiration.code');
    		return false;
    	}
    	//5.加密比对该token正确性
    	$tokenMd5=$common->tokenMd5($token,$headerToken[1],$headerToken[2]);
    	if($tokenMd5!=$headerToken[0]){
    		$this->error=config('adminMessage.token.tokenCheckError.msg');
    		$this->code=config('adminMessage.token.tokenCheckError.code');
    		return false;
		}
		return true;
	}
}
?>